// -------------------------------------------------------------------------------------------------------------------
//
//  File: main.cpp
//
//  Copyright 2016 (c) Decawave Ltd, Dublin, Ireland.
//
//  All rights reserved.
//
//  Author:
//
// -------------------------------------------------------------------------------------------------------------------

#include "RTLSDisplayApplication.h"
#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
#include <QStyleFactory>
#include <QFile>
#include <QDesktopWidget>
#include <QFileInfo>
#include <QStandardPaths>
#include <QDateTime>
#include <QSharedMemory>

#include "moveaction.h"
#include "qrviz.hpp"
#include "leiliconfig.h"


std::ofstream g_OutputDebug;

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    // 加锁
    static QMutex mutex;
    mutex.lock();
    QString text;
    switch(type) {
    case QtDebugMsg:
        text = QString("Debug");
        break;
    case QtWarningMsg:
        text = QString("Warn");
        break;
    case QtCriticalMsg:
        text = QString("Error");
        break;
    case QtFatalMsg:
        text = QString("Fatal");
        break;
    default:
        text = QString("Debug");
    }

    QString context_info = QString("%1:%2:%3").arg(QFileInfo(context.file).fileName()).arg(context.function).arg(context.line); // F文件信息L行数
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    std::string message = qPrintable(QString("[%1] [%2] [%3] \t%4").arg(current_date_time).arg(text).arg(context_info).arg(msg));

    //输入到文件
    g_OutputDebug << message << "\r\n"; // std::ofstream
    // 解锁
    mutex.unlock();

    //输出到控制台
    fprintf(stderr, "%s\n", message.c_str());
    fflush(stderr);
}

int main(int argc, char *argv[])
{

    //日志设置
    QString logName = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss").append("-log.txt");
    QString tempPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
    logName =tempPath + "/" + logName ;
    qDebug() << "log Path:" << logName;
    qInstallMessageHandler(outputMessage);
    g_OutputDebug.open(logName.toStdString(), std::ios::out | std::ios::app);

    qputenv("QT_ENABLE_HIGHDPI_SCALING", "1");
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);

    const char* MEM_KEY = "leili_d3aa15d6-33d0-42e5-bc1a-5a6c881de993";
    QSharedMemory sharedMem(MEM_KEY);
    if (sharedMem.attach()) {
        qDebug() << "雷力路径规划:程序已在运行！";
        return 0;
    }

   if (!sharedMem.create(1)) {
        qDebug() << "无法创建共享内存！";
        return 1;
    }

   //QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());

    LeiliCfg->load("./config/leiliconfig.ini");

    QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
    RTLSDisplayApplication app(argc, argv);

    // 退出时清理
    QObject::connect(&app, &QApplication::aboutToQuit, [&](){
           sharedMem.detach();
     });


    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    QApplication::setStyle(QStyleFactory::create("fusion"));




//    //app.mainWindow()->setWindowFlags(app.mainWindow()->windowFlags()& ~Qt::WindowMaximizeButtonHint);
//    //app.mainWindow()->setFixedSize(app.mainWindow()->width(), app.mainWindow()->height());
    app.mainWindow()->setWindowState(Qt::WindowMaximized);
//    //app.mainWindow()->setFixedSize(QApplication::desktop()->width(),QApplication::desktop()->height());


    app.mainWindow()->show();

    return app.QApplication::exec();
    //return a.exec();
}
